<template>
    <div class="app-container">
        <el-row
            :gutter="10"
            class="mb8 clearfix "
            style="margin-top:2px;"
            
        >
            <el-col :span="18">
                <el-form
                    :model="queryParams"
                    ref="queryForm"
                    v-show="showSearch"
                    :inline="true"
                >
                    <el-form-item
                        label="方法名称"
                        prop="methodName"
                    >
                        <el-input
                            v-model="queryParams.methodName"
                            placeholder="请输入方法名称"
                            clearable
                            size="small"
                            style="width: 240px"
                        />
                    </el-form-item>
                    <el-form-item>
                        <el-button
                            type="primary"
                            icon="el-icon-search"
                            size="mini"
                            @click="handleQuery"
                        >搜索</el-button>
                        <el-button
                            icon="el-icon-refresh"
                            size="mini"
                            @click="resetQuery"
                        >重置</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
            <el-col
                :span="1.5"
                style="float:right;margin:8px 5px 0px 0px;"
            >
                <el-button
                    type="primary"
                    icon="el-icon-plus"
                    size="small"
                    @click="handleAdd($event)"
                >添加方法</el-button>
            </el-col>
        </el-row>
        <div
            class="table"
            style="height:100%;background:white;padding:20px"
        >
            <el-table
                v-loading="loading"
                :data="methodList"
                :header-cell-style="{background:'#DADDE9',color:'#000',height:'44px'}"
                row-key="taskId"
                :tree-props="{children:'methodTasks'}"
                class="zoneTask"
                max-height="650"
            >
               
                <el-table-column
                    label="方法名称"
                    prop="taskName"
                    :show-overflow-tooltip="true"
                    width="200"
                />
                <el-table-column
                    label="说明"
                    :show-overflow-tooltip="true"
                    width="200"
                >
                <template slot-scope="scope">
                    <div v-if="scope.row.methodExplain">
                            <span>{{ scope.row.methodExplain }}</span>  
                    </div>
                    <div v-else>
                            <span>{{ scope.row.taskExplain }}</span>  
                    </div>
                </template>
                </el-table-column>
                <el-table-column
                    label="状态"
                    prop="createName"
                    align="center">
                    <template slot-scope="scope">
                            <div v-if="scope.row.status!=undefined">
                                <el-switch v-model="scope.row.blStaus" active-color="#13ce66" inactive-color="#ff4949" @change="swichChange(scope.row)"></el-switch>
                            </div>
                            <div v-else>
                                    <span></span>  
                            </div>
                    </template>
                </el-table-column>
                 <el-table-column
                    label="创建人"
                    prop="createBy"
                    align="center"
                />
                <el-table-column
                    label="创建时间"
                    prop="createTime"
                    align="center"
                    width="180"
                >
                    <template slot-scope="scope">
                        <span>{{ parseTime(scope.row.createTime) }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    label="修改人"
                    prop="updateBy"
                    align="center"
                />
                <el-table-column
                    label="修改时间"
                    prop="updateTime"
                    align="center"
                    width="180"
                >
                    <template slot-scope="scope">
                        <span>{{ parseTime(scope.row.updateTime) }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    label="操作"
                    align="center"
                    class-name="small-padding"
                >
                    <template slot-scope="scope">
                        <div v-if="scope.row.status!=undefined">
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="handleUpdate(scope.row)"
                        >修改</el-button>
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-delete"
                            @click="handleDelete(scope.row)"
                        >删除</el-button>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
            <el-row class="clearfix">
                <!-- <el-button
                    class="delete"
                    plain
                    icon="el-icon-delete"
                    size="mini"
                    :disabled="multiple"
                    @click="handleDelete"
                >删除</el-button> -->
                <pagination
                    v-show="total>0"
                    :total="total"
                    :page.sync="queryParams.pageNum"
                    :limit.sync="queryParams.pageSize"
                    @pagination="getList"
                />
            </el-row>
        </div>
        <!-- 添加或修改任务配置对话框 -->
        <el-dialog
            :title="title"
            :visible.sync="open"
            :close-on-click-modal="false"
            append-to-body
        >
        <div class="formbox">
            <el-form
                ref="form"
                :model="form"
                :rules="rules"
                label-width="100px"
            >
                <el-form-item
                    label="方法id"
                    prop="methodId"
                    v-show="false"
                >
                    <el-input v-model="form.methodId" />
                </el-form-item>
                <el-form-item
                    label="方法名称"
                    prop="methodName"
                >
                    <el-input
                        v-model="form.methodName"
                        placeholder="请输入方法名称"
                    />
                </el-form-item>       
                <el-form-item
                    label="方法说明"
                    prop="methodExplain"
                >
                    <el-input
                        v-model="form.methodExplain"
                        placeholder="请输入方法名称"
                    />
                </el-form-item>
                <el-form-item
                    label="添加步骤"
                    prop="parameter"
                >
                    <el-button icon="el-icon-plus" type="primary" @click="addParams">点击添加</el-button>    
                </el-form-item>
                <el-row v-for="(params,index) in form.methodTasks" :key="index">
                        <el-col :span="11">
                            <el-form-item label="步骤名称">
                                <el-input v-model="params.taskName" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="11">
                            <el-form-item label="步骤说明">
                                <el-input v-model="params.taskExplain" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="2">
                            <el-button
                            size="medium"
                            type="text"
                            icon="el-icon-close"
                            style="float:right;"
                            @click="removeParams(index)"
                        ></el-button>
                        </el-col>
                </el-row>
                    <el-form-item label="备注">
                        <el-input
                            v-model="form.remark"
                            type="textarea"
                            placeholder="请输入内容"
                        ></el-input>
                    </el-form-item>
            </el-form>
            </div>
            <div
                slot="footer"
                class="dialog-footer"
            >
                <el-button
                    type="primary"
                    @click="submitForm"
                >确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
            
        </el-dialog>
    </div>
</template>

<script>
import { listMethod, delMethod, addMethod, updateMethod ,updateStatus} from "@/api/method/method";
import { listDevice } from "@/api/device/device";
export default {
    name: "Method",
    data() {
        return {
            //模型方法options
            modelOptions:this.$store.getters.modelMethods,
            //关联设备options
            modelDevices:[],
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 模型表格数据
            methodList: [],
            // 显示搜索条件
            showSearch: true,
            loading: false,
            // 查询条件
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                methodName: undefined
            },
            // 总条数
            total: 0,
            // 非多个禁用
            multiple: true,
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 是否显示弹出层（数据权限）
            openDataScope: false,
            menuExpand: false,
            menuNodeAll: false,
            deptExpand: true,
            deptNodeAll: false,
            // 表单参数
            form: {
                //自定义参数
                methodTasks:[{
                    "taskName": "",
                    "taskExplain": ""
                        }]
            },
            // 表单校验
            rules: {
                methodName: [
                    { required: true, message: "方法名称不能为空", trigger: "blur" }
                ],
                methodExplain: [
                    { required: true, message: "请填写方法说明", trigger: "blur" }
                ]
            },
            value:true
        };

    },
    created() {
        this.getList();
        // this.getOptionsData();
    },
    methods: {
        /** 查询方法列表 */
        getList() {
            this.loading = true;
            listMethod(this.queryParams).then(
                response => {
                    if(response.data){
                        this.methodList = response.data.records;
                        this.methodList.map(item=>{
                            item.taskId = item.methodId
                            item.taskName = item.methodName
                            item.blStaus = item.status=='0'?false:true
                        })
                        this.total = response.data.total;
                        this.loading = false;
                    }
                }
            );
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            this.$confirm(`是否确认删除此1条模型数据项?`, "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return delMethod(row.methodId);
            }).then(() => {
                this.getList();
                this.msgSuccess("删除成功");
            }).catch(() => { })
        },
        // 表单重置
        reset() {
            if (this.$refs.menu != undefined) {
                this.$refs.menu.setCheckedKeys([]);
            }
            this.menuExpand = false,
                this.menuNodeAll = false,
                this.deptExpand = true,
                this.deptNodeAll = false,
                this.form = {
                    methodTasks:[{
                            "paramName": "",
                            "paramValue": ""
                                }],
                };
            this.resetForm("form");
        },
        /** 新增按钮操作 */
        handleAdd() {
            // getOptionsData();
            this.reset();
            this.open = true;
            this.title = "添加方法";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            // getOptionsData();
            this.reset();
            this.form =JSON.parse(JSON.stringify(row)) ;
            this.open = true;
            this.title = "修改方法信息";
        },

        created() {
            this.getList();
        },
        /** 提交按钮 */
        submitForm: function () {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.methodId != undefined) {
                        updateMethod(this.form).then(response => {
                            this.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addMethod(this.form).then(response => {
                            this.msgSuccess("添加成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            })
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        //添加参数
        addParams(){
        const params={
          "taskName": "",
          "taskExplain": ""
            };
      
        this.form.methodTasks.push(params)
        },
        //移除参数
        removeParams(id){
            
            this.form.methodTasks.splice(id,1)
        },
        swichChange(row){
            row.status = row.status=='0'?'1':'0'
            updateStatus(row.methodId)
        }
    }
}
</script>
<style scoped>
.description {
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
    word-break: break-all;
    margin-block-end: 0px;
    margin-block-start: 0px;
}
.clearfix:before,
.clearfix:after {
    display: table;
    content: ""; /*不用有内容也可以*/
}

.clearfix:after {
    clear: both;
}

.clearfix {
    *zoom: 1;
}
.el-form-item{
    margin-bottom: 18px;
}
.delete {
    margin-top: 25px;
    margin-left: 1px;
}
.formbox{
    padding:20px 20px;
}
</style>

